package ru.tennisworld.user.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.tennisworld.user.enums.ValidationErrorEnum;
import ru.tennisworld.user.entities.TennisUser;
import ru.tennisworld.user.entities.TennisUserProfile;
import ru.tennisworld.user.exceptions.TennisUserValidationException;
import ru.tennisworld.user.service.ValidationService;
import ru.tennisworld.user.service.users.TennisUserService;
import ru.tennisworld.user.utils.ValidationResult;

import java.time.LocalDate;
import java.time.Period;
import java.util.Objects;

@Service("validationService")
public class ValidationServiceImpl implements ValidationService {

    @Autowired
    private TennisUserService userService;

    private final static Logger LOG = LoggerFactory.getLogger(ValidationServiceImpl.class);

    @Override
    public void validateTennisUser(TennisUser tennisUser) {
        ValidationResult validationResult = new ValidationResult();
        validationResult.mergeValidation(validateTennisUserInfo(tennisUser));
        validationResult.mergeValidation(validateTennisUserProfile(tennisUser.getTennisUserProfile()));
        if (validationResult.isValidationError()) {
            throw new TennisUserValidationException("Ошибка валидации пользователя", validationResult);
        }
    }

    @Override
    public void validateTennisUserMainInfo(TennisUser tennisUser) {
        ValidationResult validationResult = new ValidationResult();
        validationResult.mergeValidation(validateTennisUserInfo(tennisUser));
        if (validationResult.isValidationError()) {
            throw new TennisUserValidationException("Ошибка валидации пользователя", validationResult);
        }
    }

    private ValidationResult validateTennisUserInfo(TennisUser tennisUser) {
        ValidationResult validationResult = new ValidationResult();
        if (StringUtils.isEmpty(tennisUser.getLogin())) {
            validationResult.addError(ValidationErrorEnum.TENNIS_USER_LOGIN_NULL);
        } else if (userService.checkUserDuplication(tennisUser.getLogin())) {
            validationResult.addError(ValidationErrorEnum.TENNIS_USER_DUPLICATION_BY_USERNAME);
        }
        if (StringUtils.isEmpty(tennisUser.getPassword())) {
            validationResult.addError(ValidationErrorEnum.TENNIS_USER_PASSWORD_NULL);
        }
        return validationResult;
    }

    @Override
    public void validateTennisUserProfileInfo(TennisUserProfile tennisUserProfile) {
        ValidationResult validationResult = new ValidationResult();
        validationResult.mergeValidation(validateTennisUserProfile(tennisUserProfile));
        if (validationResult.isValidationError()) {
            throw new TennisUserValidationException("Ошибка валидации профиля пользователя", validationResult);
        }
    }

    private ValidationResult validateTennisUserProfile(TennisUserProfile tennisUserProfile) {
        ValidationResult validationResult = new ValidationResult();
        if (tennisUserProfile == null) {
            validationResult.addError(ValidationErrorEnum.TENNIS_USER_PROFILE_NULL);
        } else {
            if (Objects.isNull(tennisUserProfile.getBirthday())) {
                validationResult.addError(ValidationErrorEnum.TENNIS_USER_PROFILE_BIRTHDAY_NULL);
            } else if (Period.between(tennisUserProfile.getBirthday(), LocalDate.now()).getYears() < 14) {
                validationResult.addError(ValidationErrorEnum.TENNIS_USER_PROFILE_BIRTHDAY_VALUE);
            }
        }
        return validationResult;
    }

}
